package martial.arts.tracker

import org.springframework.dao.DataIntegrityViolationException

class ReportPaymentsController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        def accountsCriteria = Account.createCriteria()

        def overdueAccounts = accountsCriteria.list  {
             payments {
                  eq("paid", false)
             }
        }
        println "getting hererererereer"
        overdueAccounts.each { i -> println i.accountId}

       /* def overduePaymentsList = Payment.findAllByPaid(false)

        println "size of overdue payments " + overduePaymentsList.size()

        overduePaymentsList.each { i -> println i.paymentRule}

        def overdueAccountsList = overduePaymentsList ? Account.findAllByPayments(overduePaymentsList) : []

//        overdueAccountsList.each { i -> println i.getAccountId}*/

        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [reportPaymentsInstanceList: overdueAccounts, reportPaymentsInstanceTotal: overdueAccounts.size()]
    }

    def create() {
        [reportPaymentsInstance: new ReportPayments(params)]
    }

    def save() {
        def reportPaymentsInstance = new ReportPayments(params)
        if (!reportPaymentsInstance.save(flush: true)) {
            render(view: "create", model: [reportPaymentsInstance: reportPaymentsInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), reportPaymentsInstance.id])
        redirect(action: "show", id: reportPaymentsInstance.id)
    }

    def show() {
        def reportPaymentsInstance = ReportPayments.get(params.id)
        if (!reportPaymentsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), params.id])
            redirect(action: "list")
            return
        }

        [reportPaymentsInstance: reportPaymentsInstance]
    }

    def edit() {
        def reportPaymentsInstance = ReportPayments.get(params.id)
        if (!reportPaymentsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), params.id])
            redirect(action: "list")
            return
        }

        [reportPaymentsInstance: reportPaymentsInstance]
    }

    def update() {
        def reportPaymentsInstance = ReportPayments.get(params.id)
        if (!reportPaymentsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (reportPaymentsInstance.version > version) {
                reportPaymentsInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'reportPayments.label', default: 'ReportPayments')] as Object[],
                        "Another user has updated this ReportPayments while you were editing")
                render(view: "edit", model: [reportPaymentsInstance: reportPaymentsInstance])
                return
            }
        }

        reportPaymentsInstance.properties = params

        if (!reportPaymentsInstance.save(flush: true)) {
            render(view: "edit", model: [reportPaymentsInstance: reportPaymentsInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), reportPaymentsInstance.id])
        redirect(action: "show", id: reportPaymentsInstance.id)
    }

    def delete() {
        def reportPaymentsInstance = ReportPayments.get(params.id)
        if (!reportPaymentsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), params.id])
            redirect(action: "list")
            return
        }

        try {
            reportPaymentsInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'reportPayments.label', default: 'ReportPayments'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
